/**
 * Created by Mark_ren on 2017/6/5.
 * 苟富贵,無相忘
 */
$(function () {

    var sel = $.Params.create();
    var liveId = getQueryString("liveId");
    getData();
    //查询
    $("#searchBtn").on("click", function () {
        var searchVal = $("#searchVal").val();
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        if (searchVal != "") {
            sel.addParams("pTitle", searchVal);
        } else {
            sel.addParams("pTitle", undefined);
        }

        if (startTime != "") {
            sel.addParams("startTime", startTime + " 00:00:00");
        } else {
            sel.addParams("startTime", undefined);
        }

        if (endTime != "") {
            sel.addParams("endTime", endTime + " 23:59:59");
        } else {
            sel.addParams("endTime", undefined);
        }
        getData();
    })

    //导出
    $("#expBtn").on("click", function () {
        var url = getAdminIP() + "signUps/export/" + liveId + "?1=1"
        var searchVal = $("#searchVal").val();
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        if (searchVal != "") {
            url += " &pTitle=" + searchVal;
        }

        if (startTime != "") {
            url += " &startTime=" + startTime + " 00:00:00";
        }

        if (endTime != "") {
            url += " &endTime=" + endTime + " 23:59:50";
        }
        if (confirm("确定要导出吗！")) {
            window.location.href = url;
        }
    })

    //判断checkbox状态
    $("#selAll").on("click", function () {
        if (!$(this).attr("checked")) {
            $("input[type=checkbox]").attr("checked", "checked")
        } else {
            $("input[type=checkbox]").removeAttr("checked");
        }
    })
    //删除事件
    $('#delBtn').on("click", function () {
        var pararms = getCheckData();
        delData(pararms);
    });

    //删除数据
    function delData(ids) {
        if (!ids == "") {
            if (confirm("确定要删除吗！")) {
                var del = $.Params.create();
                del.addParams("params", ids)
                del.ajax("delete", getAdminIP() + "signUps?params=" + ids, function (data) {
                    if (data.success) {
                        alert(data.msg);
                        getData();
                    }
                })
            }
        } else {
            alert("请至少选择一项");
        }
    }

    //获取数据
    function getData() {
        sel.addParams("page", 0);
        sel.addParams("rows", rows);
        getPageData("get", sel, getAdminIP() + "signUps/" + liveId, function (data) {
            if (data.success) {
                var html = "";
                $.each(data.result, function (i, val) {
                    html += '<tr role="row" class="odd">' +
                        "<td><input eid='" + val.id + "' onclick='getCheck(this)' type='checkbox' class='classOption'></td>";
                    if (val.member != null) {
                        html += "<td>" + val.member.name + "</td><td>" + val.member.tel + "</td><td> " + val.member.hospitalName + "</td> ";
                    } else {
                        html += "<td></td><td></td><td></td>";
                    }
                    html += "<td>" + val.createTime + "</td>" +
                        "</tr>";

                });
                $("#listContainer").html(html);
            }
        })
    }
});
